package fr.n7.sma.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import fr.n7.sma.model.Agent;
import fr.n7.sma.model.listeners.AgentListener;
import fr.n7.sma.model.listeners.events.AgentEvent;
import fr.n7.sma.util.property.PropertyChangedEvent;
import fr.n7.sma.util.property.PropertyListener;
import fr.n7.sma.util.property.PropertyManager;
import fr.n7.sma.view.Grid.Properties;
import fr.n7.sma.view.listeners.AgentViewPropertiesListener;
import fr.n7.sma.view.model.AgentViewProperties;
import fr.n7.sma.view.model.CellViewProperties;

public class AgentPanel extends JPanel 
	implements 	AgentViewPropertiesListener,
				AgentListener<AgentViewProperties, CellViewProperties>,
				PropertyListener<Properties>{

	private static final long serialVersionUID = 2170110776205226469L;
	private Agent<AgentViewProperties, CellViewProperties> agent;
	private PropertyManager<Properties> props;
	private Map<String, Integer> resourceNameToIndex;
	
	private JLabel icon;
	private JTextField actualAction;
	private JTextField lifeTxtField;
	private JTextField resourcesTxtField;
	private StringBuffer agentLife;
	private StringBuffer agentResources;
	
	
	@SuppressWarnings("unchecked")
	public AgentPanel(Agent<AgentViewProperties, CellViewProperties> a, PropertyManager<Properties> props){
		this.agent = a;
		this.props = props;
		this.resourceNameToIndex = new HashMap<String, Integer>();
		this.agentLife = new StringBuffer();
		majResourceNameToIndex();
		props.addListener(Properties.RESOURCES, List.class, this);
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		
		icon = new JLabel(a.getName(), agent.getAdditionnalProperty().getLabel().getIcon(), JLabel.HORIZONTAL);
		icon.setMinimumSize(new Dimension(80, 10));
		icon.setPreferredSize(new Dimension(80, icon.getPreferredSize().height));
		icon.addMouseListener(new IconMouseListener());
		
		actualAction = new JTextField("-", 3);
		actualAction.setMinimumSize(new Dimension(20, 10));
		actualAction.setPreferredSize(new Dimension(20, actualAction.getPreferredSize().height));
		actualAction.setMaximumSize(new Dimension(20, 100));
		actualAction.setHorizontalAlignment(JTextField.CENTER);
		Font font = new Font(actualAction.getFont().getFontName(), Font.BOLD, 16);
		actualAction.setFont(font);
		
		lifeTxtField = new JTextField();
		lifeTxtField.setFont(new Font("monospaced", Font.PLAIN, 13));
		
		resourcesTxtField = new JTextField();
		resourcesTxtField.setFont(new Font("monospaced", Font.PLAIN, 13));
		
		a.getAdditionnalProperty().addListener(this);
		add(icon);
		add(Box.createRigidArea(new Dimension(10,1)));
		add(Box.createHorizontalGlue());
		add(actualAction);
		add(Box.createHorizontalGlue());
		
		JPanel life = new JPanel();
		life.setLayout(new BoxLayout(life, BoxLayout.Y_AXIS));
		life.add(Box.createVerticalGlue());
		life.add(lifeTxtField);
		life.add(resourcesTxtField);
		life.add(Box.createVerticalGlue());
		add(life);
	}
	
	@SuppressWarnings("unchecked")
	private void majResourceNameToIndex(){
		int i = 0;
		for(String s : (List<String>)props.get(Properties.RESOURCES, List.class)){
			resourceNameToIndex.put(s, i++);
		}
	}
	
	public void setContinuousLifeRefresh(boolean b){
		if(b){
			agentLife = new StringBuffer();
			lifeTxtField.setText("");
			agentResources = new StringBuffer();
			resourcesTxtField.setText("");
			agent.addAgentListener(this);
		}else{
			agent.removeAgentListener(this);
		}
	}

	public void agentViewPropertiesChanged() {
		icon.setIcon(agent.getAdditionnalProperty().getLabel().getIcon());
	}

	public void agentChanged(
			AgentEvent<AgentViewProperties, CellViewProperties> event) {
		switch(event.getEventType()){
		case AGENT_MOVED :
		case RESOURCE_PUT :
		case RESOURCE_TAKEN :
		{
			String lastAction = agent.getLastAction().toString(resourceNameToIndex);
			int length = agent.getLife().getMaxLength()*3;
			
			agentLife.insert(0, lastAction+":");
			agentLife.setLength(length);
			lifeTxtField.setText(agentLife.toString());
			
			Integer iResHeld = resourceNameToIndex.get(agent.getHeldResourceName());
			agentResources.insert(0, ((iResHeld == null)?"--":("r"+iResHeld))+":");
			agentResources.setLength(length);
			resourcesTxtField.setText(agentResources.toString());
			
			actualAction.setText(lastAction);
			break;
		} 
		default :
		}
	}

	public void propertyChanged(PropertyChangedEvent<Properties> e) {
		switch(e.getKey()){
		case RESOURCES:
			majResourceNameToIndex();
			break;
		}
	}
	
	private class IconMouseListener implements MouseListener{

		
		public void mouseClicked(MouseEvent arg0) {
			Color color = JColorChooser.showDialog(AgentPanel.this, "Choisissez la couleur de votre agent.", agent.getAdditionnalProperty().getColor());
			agent.getAdditionnalProperty().setColor(color);
			props.notifyListeners(Properties.DRAW_IMAGES_FOR_AGENTS, Boolean.class);
		}

		
		public void mouseEntered(MouseEvent arg0) {
		}

		
		public void mouseExited(MouseEvent arg0) {
		}

		
		public void mousePressed(MouseEvent arg0) {
		}

		
		public void mouseReleased(MouseEvent arg0) {
		}
		
	}
	
}
